package com.smxy.mall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .pathMapping("/")
                .globalOperationParameters(getGlobalOperationParameters()) //全局参数
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.smxy.mall.controller"))
                .paths(PathSelectors.any())
                .build()
                .apiInfo(apiInfo());
    }

    private List<Parameter> getGlobalOperationParameters() {
        List<Parameter> pars = new ArrayList<>();
        ParameterBuilder parameterBuilder = new ParameterBuilder();
        // 添加header参数，为了测试方便，添加一个默认token
        parameterBuilder.name("token")
                .defaultValue("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0eXBlIjoiYWRtaW4iLCJ1c2VySWQiOjM0LCJwaG9uZSI6IjE1NzE2MDIyMzk5IiwidXNlck5hbWUiOiJhZG1pbiIsInN1YiI6IjM0IiwiaXNzIjoiMDk4ZjZiY2Q0NjIxZDM3M2NhZGU0ZTgzMjYyN2I0ZjYiLCJpYXQiOjE2MDc1NzExNDUsImF1ZCI6InJlc3RhcGl1c2VyIiwiZXhwIjoxNjA4MTc1OTQ1LCJuYmYiOjE2MDc1NzExNDV9.xf3vEfi2ZlqUMJyi-kd1g_EiSJbs3OrNpVcuGMMPP38")
                .modelRef(new ModelRef("string"))
                .parameterType("header")
                .required(false);
        pars.add(parameterBuilder.build());
        return pars;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口文档")
                .description("springboot+vue")
                .version("1.0")
                .contact(new Contact("帅小黑x","blog.csdn.net","aaa@gmail.com"))
                .license("The Apache License")
                .licenseUrl("http://www.baidu.com")
                .build();
    }

}

